/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerLogin;
import enun.Perfil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import model.Login;
import util.MascaraUtil;
import util.MensagemUtil;

/**
 *
 * @author Rodrigo
 */
public class ViewLogin extends javax.swing.JFrame {

    private final ControllerLogin controllerLogin;
    private final Login login;

    public ViewLogin() {
        this(new Login());
    }

    public ViewLogin(Login l) {
        controllerLogin = new ControllerLogin();
        this.login = l;
        initComponents();
        lblLogado.setText(login.getPerfil().getDescricao());
        criarCamboPerfil();
        visibilidadeCamposAlterarSenha(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        comboPerfil = new javax.swing.JComboBox();
        txtSenha = new javax.swing.JTextField();
        btnCadastrar = new javax.swing.JButton();
        lblPerfil = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        btnSair = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblusuarioLogado = new javax.swing.JLabel();
        lblLogado = new javax.swing.JLabel();
        txtTrocarSenha = new javax.swing.JTextField();
        lblNovaSenha = new javax.swing.JLabel();
        checkHabilitarSenha = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSenhaActionPerformed(evt);
            }
        });

        btnCadastrar.setText("Cadastrar/Alterar");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        lblPerfil.setText("Perfil:");

        lblSenha.setText("Senha:");

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Cadastrar Perfil");

        lblusuarioLogado.setText("Usuario logado:");

        lblLogado.setText("-");

        lblNovaSenha.setText("Nova Senha:");

        checkHabilitarSenha.setText("habilitar troca de senha");
        checkHabilitarSenha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkHabilitarSenhaMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblusuarioLogado)
                        .addGap(18, 18, 18)
                        .addComponent(lblLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNovaSenha)
                            .addComponent(jLabel1)
                            .addComponent(comboPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPerfil)
                            .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSenha)
                            .addComponent(txtTrocarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 158, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(checkHabilitarSenha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSair)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblusuarioLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLogado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblPerfil)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblSenha)
                .addGap(11, 11, 11)
                .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTrocarSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addComponent(btnCadastrar)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair)
                    .addComponent(checkHabilitarSenha))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSenhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSenhaActionPerformed

    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        salvar();
    }//GEN-LAST:event_btnCadastrarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void checkHabilitarSenhaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkHabilitarSenhaMouseClicked
        if (checkHabilitarSenha.isSelected()) {
            visibilidadeCamposAlterarSenha(true);
        } else {
            visibilidadeCamposAlterarSenha(false);
        }

    }//GEN-LAST:event_checkHabilitarSenhaMouseClicked

    private void carregarDadosLogin() {
        login.setPerfil((Perfil) comboPerfil.getSelectedItem());
        MascaraUtil.limitarCampoTexto(txtSenha, "Senha", 10);
        login.setSenha(txtSenha.getText());
    }

    private void salvar() {
        if (validarCampos()) {
            if (!checkHabilitarSenha.isSelected()) {
                carregarDadosLogin();
                controllerLogin.cadastrarLogin(login);
                limparCamposLogin();

            } else {
                login.setNovaSenha(txtTrocarSenha.getText());
                carregarDadosLogin();
                if (controllerLogin.alterar(login)) {
                    limparCamposLogin();
                }

            }
        }

    }

    private void limparCamposLogin() {
        txtSenha.setText("");
        comboPerfil.setSelectedItem(null);
        txtTrocarSenha.setText("");
        criarCamboPerfil();
    }

    private boolean validarCampos() {
        boolean isValido = true;

        if (comboPerfil.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Perfil");
        }
        if (txtSenha.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("Senha");
        }
        
          if (txtTrocarSenha.getText().equals("") && checkHabilitarSenha.isSelected()) {
            isValido = false;
            MensagemUtil.msgPadrao("Nova Senha:");
        }
        

        return isValido;
    }

    private void criarCamboPerfil() {
        List<Perfil> listaPerfil = new ArrayList<>();
        listaPerfil = Arrays.asList(Perfil.values());
        comboPerfil.removeAllItems();;
        comboPerfil.addItem("Selecione");
        for (Perfil perfil : listaPerfil) {
            comboPerfil.addItem(perfil);
        }
    }

    private void visibilidadeCamposAlterarSenha(boolean habilitar) {
        txtTrocarSenha.setVisible(habilitar);
        lblNovaSenha.setVisible(habilitar);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnSair;
    private javax.swing.JCheckBox checkHabilitarSenha;
    private javax.swing.JComboBox comboPerfil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblLogado;
    private javax.swing.JLabel lblNovaSenha;
    private javax.swing.JLabel lblPerfil;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JLabel lblusuarioLogado;
    private javax.swing.JTextField txtSenha;
    private javax.swing.JTextField txtTrocarSenha;
    // End of variables declaration//GEN-END:variables
}
